﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Accountant_BankTableDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetValue();
        }
    }


    private void GetValue()
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];
        double Balance;
        ExchangeRateEntity exchangeRate = null;
        List<BankTableDetailEmtity> list = new List<BankTableDetailEmtity>();
        if (Request["type"].ToString() == "US")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.US).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.US).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.US).ToString());
        }
        else if (Request["type"].ToString() == "RMB")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.RMB).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.RMB).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.RMB).ToString());
        }
        else if (Request["type"].ToString() == "GBP")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.GBP).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.GBP).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.GBP).ToString());
        }
        else if (Request["type"].ToString() == "CAD")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.CAD).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.CAD).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.CAD).ToString());
        }
        else if (Request["type"].ToString() == "CHF")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.CHF).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.CHF).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.CHF).ToString());
        }
        else if (Request["type"].ToString() == "JPY")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.JPY).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.JPY).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.JPY).ToString());
        }
        else if (Request["type"].ToString() == "AUD")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.AUD).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.AUD).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.AUD).ToString());
        }
        else if (Request["type"].ToString() == "EUR")
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.EUR).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.EUR).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.EUR).ToString());
        }

        else
        {
            list = new AuditorsBLL().GetBankTableDetail(company, orderid, ((int)CurrencyEnum.HK).ToString());
            exchangeRate = new AuditorsBLL().GetExchangeRate(orderid, ((int)CurrencyEnum.HK).ToString());
            Balance = new AuditorsBLL().GetCompanyBankMantissa(company, orderid, ((int)CurrencyEnum.HK).ToString());
        }


        //double amount = 0;
        //double hk = 0;
        //double dr = 0;
        //double cr = 0;
        //double inc = 0;
        //double cha = 0;

        if (list.Count <= 0)
        {
            Label1.Text = "暂无数据";
            return;
        }

      

        if (exchangeRate == null)
        {
            Label1.Text = "汇率异常，请联系管理员。";
            return;
        }

        StringBuilder sb = new StringBuilder();
        //sb.Append("<table>");
        //sb.Append("<tr>");
        //sb.AppendFormat("<td>{0}</td>", "Date");
        //sb.AppendFormat("<td>{0}</td>", "Amount");
        //sb.AppendFormat("<td>{0}</td>", "Balance");
        //sb.AppendFormat("<td>{0}</td>", "EX");
        //sb.AppendFormat("<td>{0}</td>", "HK");
        //sb.AppendFormat("<td>{0}</td>", "Dr");
        //sb.AppendFormat("<td>{0}</td>", "Cr");
        //sb.AppendFormat("<td>{0}</td>", "Interest income");
        //sb.AppendFormat("<td>{0}</td>", "Bank charge");
        //sb.Append("</tr>");
        //sb.Append("<tr>");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", Balance);
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.Append("</tr>");
        //foreach (var bankTableDetailEmtity in list)
        //{
        //    amount += bankTableDetailEmtity.Amount;
        //    hk += bankTableDetailEmtity.HkMoney;
        //    if (bankTableDetailEmtity.Amount > 0)
        //    {
        //        dr += bankTableDetailEmtity.Amount;
        //    }
        //    else
        //    {
        //        cr += bankTableDetailEmtity.Amount;
        //    }
        //    inc += bankTableDetailEmtity.InterestIncome;
        //    cha += bankTableDetailEmtity.BankCharge;

        //    Balance += bankTableDetailEmtity.Amount;

        //    sb.Append("<tr>");
        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Date.ToString("dd-MM-yyyy"));
        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Amount);
        //    sb.AppendFormat("<td>{0}</td>", Balance);
        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Ex);
        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.HkMoney);

        //    if (bankTableDetailEmtity.Amount > 0)
        //    {
        //        sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Amount);
        //        sb.AppendFormat("<td>{0}</td>", "-");
        //    }
        //    else
        //    {
        //        sb.AppendFormat("<td>{0}</td>", "-");
        //        sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Amount);
        //    }

        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.InterestIncome);
        //    sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.BankCharge);
        //    sb.Append("</tr>");
        //}

        //sb.Append("<tr>");
        //sb.AppendFormat("<td>{0}</td>", Request["type"].ToString());
        //sb.AppendFormat("<td>{0}</td>", amount);
        //sb.AppendFormat("<td>{0}</td>", Balance);
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", "");
        //sb.AppendFormat("<td>{0}</td>", dr);
        //sb.AppendFormat("<td>{0}</td>", cr);
        //sb.AppendFormat("<td>{0}</td>", inc);
        //sb.AppendFormat("<td>{0}</td>", cha);
        //sb.Append("</tr>");
        //sb.Append("</table>");

        sb.Append("<table>");
        sb.Append("<tr>");
        sb.AppendFormat("<td>{0}</td>", "Date");
        sb.AppendFormat("<td>{0}</td>", "Description");
        sb.AppendFormat("<td>{0}</td>", "Amount(Dr/<Cr>)");
        sb.AppendFormat("<td>{0}</td>", "Balance(HK)");
        sb.AppendFormat("<td>{0}</td>", " ");
        sb.AppendFormat("<td>{0}</td>", "Control total");
        sb.AppendFormat("<td>{0}</td>", "Sales income(Dr)");
        sb.AppendFormat("<td>{0}</td>", "Purchase(Cr)");
        sb.AppendFormat("<td>{0}</td>", "Bank interest income");
        sb.AppendFormat("<td>{0}</td>", "Bank charge");
        sb.AppendFormat("<td>{0}</td>", "Exchange difference");
        sb.Append("</tr>");

        double amount = 0, cr = 0, dr = 0, interestIncome = 0, bankCharge = 0;
        double ExchangeRate = exchangeRate.ExchangeRate;

        foreach (var bankTableDetailEmtity in list)
        {

            //balance += bankTableDetailEmtity.Amount;

            switch (bankTableDetailEmtity.Ex)
            {
                case "0":
                    dr += bankTableDetailEmtity.Amount;
                    amount += bankTableDetailEmtity.Amount;
                    break;
                case "1":
                    cr -= bankTableDetailEmtity.Amount;
                    amount -= bankTableDetailEmtity.Amount;
                    break;
                case "2":
                    interestIncome += bankTableDetailEmtity.Amount;
                    amount += bankTableDetailEmtity.Amount;
                    break;
                case "3":
                    bankCharge -= bankTableDetailEmtity.Amount;
                    amount -= bankTableDetailEmtity.Amount;
                    break;
                case "4":
                    bankCharge -= bankTableDetailEmtity.Amount;
                    amount -= bankTableDetailEmtity.Amount;
                    break;
            }

            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Date.ToString("dd-MM-yyyy"));
            sb.AppendFormat("<td>{0}</td>",
                            bankTableDetailEmtity.Ex == "3" || bankTableDetailEmtity.Ex == "4" ? "Bank  charges" : "");
            sb.AppendFormat("<td>{0}</td>", (bankTableDetailEmtity.Amount).ToString("0.00"));
            sb.AppendFormat("<td>{0}</td>", (amount).ToString("0.00"));
            sb.AppendFormat("<td>{0}</td>", " ");
            sb.AppendFormat("<td>{0}</td>", "");
            sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Ex == "0" ? bankTableDetailEmtity.Amount.ToString("0.00") : "");
            sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Ex == "1" ? bankTableDetailEmtity.Amount.ToString("0.00") : "");
            sb.AppendFormat("<td>{0}</td>", bankTableDetailEmtity.Ex == "2" ? bankTableDetailEmtity.Amount.ToString("0.00") : "");
            sb.AppendFormat("<td>{0}</td>",
                            bankTableDetailEmtity.Ex == "3" || bankTableDetailEmtity.Ex == "4"
                                ? bankTableDetailEmtity.Amount.ToString("0.00")
                                : "");
            sb.AppendFormat("<td>{0}</td>", "");
            sb.Append("</tr>");



            ExchangeRate = bankTableDetailEmtity.HkMoney / bankTableDetailEmtity.Amount;
        }


        sb.Append("<tr>");
        sb.AppendFormat("<td>{0}</td>", Request["type"].ToString());
        sb.AppendFormat("<td>{0}</td>", "(A)");
        sb.AppendFormat("<td>{0}</td>", (amount).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", "");
        sb.AppendFormat("<td>{0}</td>", "(B)");
        sb.AppendFormat("<td>{0}</td>", (dr + cr + interestIncome + bankCharge).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", (dr).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", (-cr).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", (interestIncome).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", (-bankCharge).ToString("0.00"));
        sb.AppendFormat("<td>{0}</td>", "-");
        sb.Append("</tr>");

        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            try
            {
                double sum = 0;
                sum += ExchangeRate * dr;
                sum += ExchangeRate * cr;
                sum += ExchangeRate * interestIncome;
                sum += ExchangeRate * bankCharge;

                double d = Convert.ToDouble(TextBox1.Text);
                sb.Append("<tr>");
                sb.AppendFormat("<td>{0}</td>", "@" + TextBox1.Text);
                sb.AppendFormat("<td>{0}</td>", "");
                sb.AppendFormat("<td>{0}</td>", TextBox1.Text);
                sb.AppendFormat("<td>{0}</td>", "");
                sb.AppendFormat("<td>{0}</td>", "");
                sb.AppendFormat("<td>{0}</td>", "");
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate).ToString("0.0000"));
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate).ToString("0.0000"));
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate).ToString("0.0000"));
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate).ToString("0.0000"));
                sb.AppendFormat("<td>{0}</td>", "");
                sb.Append("</tr>");

                sb.Append("<tr>");
                sb.AppendFormat("<td>{0}</td>", "HK");
                sb.AppendFormat("<td>{0}</td>", "(C)");
                sb.AppendFormat("<td>{0}</td>", (d * amount).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (amount * d).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", "(D)");
                sb.AppendFormat("<td>{0}</td>", (amount * d).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate * dr).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (-ExchangeRate * cr).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (ExchangeRate * interestIncome).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (-ExchangeRate * bankCharge).ToString("0.00"));
                sb.AppendFormat("<td>{0}</td>", (sum - amount * d).ToString("0.00"));
                sb.Append("</tr>");

            }
            catch (Exception)
            {

            }
        }
        sb.Append("</table>");

        Label1.Text = sb.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        GetValue();
    }
}